package seqlist.leetcode;

public class jianzhioffer22 {
    public ListNode1 getKthFromEnd(ListNode1 head, int k) {
//        int count=0;
//       for (ListNode1 x=head;x!=null;x=x.next){
//           count++;
//       }
//       int jindex=count-k;
//       ListNode1 j = head;
//        for (int i = 0; i < jindex; i++) {
//            head=head.next;
//        }
//        return head;
        if (head==null||k<=0){
            return null;
        }
           ListNode1 fir=head;
           ListNode1 sec=head;
           int count = 0;
           while (fir!=null){
               fir=fir.next;
               count++;
               if (count>=k+1){
                   sec=sec.next;
               }
           }
           //k>count,不存在解
        if (k>count){
            return null;
        }
        return sec;
    }
}
